box::use(dplyr[...])

df_clean <- readRDS("data/df_clean.RDS")

df_clean$hCountry <- factor(df_clean$hCountry)
levels(df_clean$hCountry)[levels(df_clean$hCountry)=="1"] <- "Germany"
levels(df_clean$hCountry)[levels(df_clean$hCountry)=="2"] <- "France"


df_clean <- df_clean %>% 
  mutate(Education = ifelse(hCountry == "France", edulevel_FR, edulevel))

df_clean$Education <- factor(df_clean$Education)

levels(df_clean$Education)[levels(df_clean$Education)=="1"] <- "Low"
levels(df_clean$Education)[levels(df_clean$Education)=="2"] <- "Middle"
levels(df_clean$Education)[levels(df_clean$Education)=="3"] <- "High"


### Gender

df_clean$Gender <- factor(df_clean$Q4)

levels(df_clean$Gender)[levels(df_clean$Gender)=="1"] <- "Male"
levels(df_clean$Gender)[levels(df_clean$Gender)=="2"] <- "Female"
levels(df_clean$Gender)[levels(df_clean$Gender)=="3"] <- "Other"

### Age

df_clean$Age <- factor(df_clean$hAge)
levels(df_clean$Age)[levels(df_clean$Age)=="1"] <- "18-24"
levels(df_clean$Age)[levels(df_clean$Age)=="2"] <- "25-34"
levels(df_clean$Age)[levels(df_clean$Age)=="3"] <- "35-44"
levels(df_clean$Age)[levels(df_clean$Age)=="4"] <- "45-54"
levels(df_clean$Age)[levels(df_clean$Age)=="5"] <- "55-64"
levels(df_clean$Age)[levels(df_clean$Age)=="6"] <- "65-120"


### Region

df_clean$region_de <- factor(df_clean$Q5_DE)

levels(df_clean$region_de)[levels(df_clean$region_de)=="1"] <- "Baden-Württemberg"
levels(df_clean$region_de)[levels(df_clean$region_de)=="2"] <- "Bayern"
levels(df_clean$region_de)[levels(df_clean$region_de)=="3"] <- "Berlin"
levels(df_clean$region_de)[levels(df_clean$region_de)=="4"] <- "Brandenburg"
levels(df_clean$region_de)[levels(df_clean$region_de)=="5"] <- "Bremen"
levels(df_clean$region_de)[levels(df_clean$region_de)=="6"] <- "Hamburg"
levels(df_clean$region_de)[levels(df_clean$region_de)=="7"] <- "Hessen"
levels(df_clean$region_de)[levels(df_clean$region_de)=="8"] <- "Mecklenburg-Vorpommern"
levels(df_clean$region_de)[levels(df_clean$region_de)=="9"] <- "Niedersachsen"
levels(df_clean$region_de)[levels(df_clean$region_de)=="10"] <- "Nordrhein-Westfalen"
levels(df_clean$region_de)[levels(df_clean$region_de)=="11"] <- "Rheinland-Pfalz"
levels(df_clean$region_de)[levels(df_clean$region_de)=="12"] <- "Saarland"
levels(df_clean$region_de)[levels(df_clean$region_de)=="13"] <- "Sachsen"
levels(df_clean$region_de)[levels(df_clean$region_de)=="14"] <- "Sachsen-Anhalt"
levels(df_clean$region_de)[levels(df_clean$region_de)=="15"] <- "Schleswig-Holstein"
levels(df_clean$region_de)[levels(df_clean$region_de)=="16"] <- "Thüringen"


df_clean$region_fr <- factor(df_clean$Q5_FR)

levels(df_clean$region_fr)[levels(df_clean$region_fr)=="1"] <- "Auvergne-Rhone-Alpes"
levels(df_clean$region_fr)[levels(df_clean$region_fr)=="2"] <- "Bourgogne-Franche-Comte"
levels(df_clean$region_fr)[levels(df_clean$region_fr)=="3"] <- "Bretagne"
levels(df_clean$region_fr)[levels(df_clean$region_fr)=="4"] <- "Centre-Val de Loire"
levels(df_clean$region_fr)[levels(df_clean$region_fr)=="5"] <- "Corse"
levels(df_clean$region_fr)[levels(df_clean$region_fr)=="6"] <- "Grand Est"
levels(df_clean$region_fr)[levels(df_clean$region_fr)=="7"] <- "Hauts-de-France"
levels(df_clean$region_fr)[levels(df_clean$region_fr)=="8"] <- "Ile-de-France"
levels(df_clean$region_fr)[levels(df_clean$region_fr)=="9"] <- "Normandie"
levels(df_clean$region_fr)[levels(df_clean$region_fr)=="10"] <- "Nouvelle-Aquitaine"
levels(df_clean$region_fr)[levels(df_clean$region_fr)=="11"] <- "Okzitanien"
levels(df_clean$region_fr)[levels(df_clean$region_fr)=="12"] <- "Pays de la Loire"
levels(df_clean$region_fr)[levels(df_clean$region_fr)=="13"] <- "Provence-Alpes-Cote d'Azur"



df_clean <- df_clean %>%
  mutate(Household_Size = Q8r1 + Q8r2)

df_clean <- df_clean %>% 
  mutate(Household_Income = ifelse(Q9 == 1 | Q9 == 2 | Q9 == 3, 1, 0)) %>% 
  mutate(Household_Income = ifelse(Q9 == 4 | Q9 == 5 | Q9 == 6, 2, Household_Income)) %>% 
  mutate(Household_Income = ifelse(Q9 == 7 | Q9 == 8 , 3, Household_Income)) %>%
  mutate(Household_Income = ifelse(Q9 == 9 | Q9 == 10, 4, Household_Income)) %>% 
  mutate(Household_Income = ifelse(Q9 == 11 | Q9 == 12 , 5, Household_Income)) %>%
  mutate(Household_Income = ifelse(Q9 == 13, 6, Household_Income)) %>%
  mutate(Household_Income = ifelse(Q9 == 14 | Q9 == 15, 7, Household_Income)) %>%
  mutate(Household_Income = ifelse(Q9 == 16 | Q9 == 17, NA, Household_Income))

df_clean$Household_Income <- factor(df_clean$Household_Income)


levels(df_clean$Household_Income)[levels(df_clean$Household_Income)=="1"] <- "0-1000 Euro"
levels(df_clean$Household_Income)[levels(df_clean$Household_Income)=="2"] <- "1000-1750 Euro"
levels(df_clean$Household_Income)[levels(df_clean$Household_Income)=="3"] <- "1750-2500 Euro"
levels(df_clean$Household_Income)[levels(df_clean$Household_Income)=="4"] <- "2500-3500 Euro"
levels(df_clean$Household_Income)[levels(df_clean$Household_Income)=="5"] <- "3500-5000 Euro"
levels(df_clean$Household_Income)[levels(df_clean$Household_Income)=="6"] <- "5000-7500 Euro"
levels(df_clean$Household_Income)[levels(df_clean$Household_Income)=="7"] <- "7500 Euro and more"



df_clean$Job_situation <- factor(df_clean$Q7)

levels(df_clean$Job_situation)[levels(df_clean$Job_situation)=="1"] <- "Employed"
levels(df_clean$Job_situation)[levels(df_clean$Job_situation)=="2"] <- "Part-time employed"
levels(df_clean$Job_situation)[levels(df_clean$Job_situation)=="3"] <- "In training, Student, in voluntary service or similar"
levels(df_clean$Job_situation)[levels(df_clean$Job_situation)=="4"] <- "Currently unemployed/looking for work"
levels(df_clean$Job_situation)[levels(df_clean$Job_situation)=="5"] <- "Pensioner/retiree"
levels(df_clean$Job_situation)[levels(df_clean$Job_situation)=="6"] <- "Housework, care of children or other persons"
levels(df_clean$Job_situation)[levels(df_clean$Job_situation)=="7"] <- "Unable to work"
levels(df_clean$Job_situation)[levels(df_clean$Job_situation)=="8"] <- "Other"
levels(df_clean$Job_situation)[levels(df_clean$Job_situation)=="9"] <- NA


df_clean <- df_clean %>% 
  mutate(Econ_self = Q10)

df_clean <- df_clean %>% 
  mutate(Econ_country = Q11)

df_clean <- df_clean %>% 
  mutate(Pol_Interest = Q12)

df_clean <- df_clean %>% 
  mutate(party_choice_de = Q13)

df_clean <- df_clean %>% 
  mutate(lr = Q14)



df_clean$party_choice_de<- factor(df_clean$party_choice_de)
levels(df_clean$party_choice_de)[levels(df_clean$party_choice_de)=="1"] <- "CDU/CSU"
levels(df_clean$party_choice_de)[levels(df_clean$party_choice_de)=="2"] <- "SPD"
levels(df_clean$party_choice_de)[levels(df_clean$party_choice_de)=="3"] <- "Die Grünen"
levels(df_clean$party_choice_de)[levels(df_clean$party_choice_de)=="4"] <- "DIE LINKE"
levels(df_clean$party_choice_de)[levels(df_clean$party_choice_de)=="5"] <- "FDP"
levels(df_clean$party_choice_de)[levels(df_clean$party_choice_de)=="6"] <- "AfD"
levels(df_clean$party_choice_de)[levels(df_clean$party_choice_de)=="7"] <- "Other"
levels(df_clean$party_choice_de)[levels(df_clean$party_choice_de)=="8"] <- NA


df_clean <- df_clean %>% 
  mutate(party_choice_fr = Q13_FR)

df_clean$party_choice_fr<- factor(df_clean$party_choice_fr)
levels(df_clean$party_choice_fr)[levels(df_clean$party_choice_fr)=="1"] <- "Rassemblement national"
levels(df_clean$party_choice_fr)[levels(df_clean$party_choice_fr)=="2"] <- "La Republique en marche"
levels(df_clean$party_choice_fr)[levels(df_clean$party_choice_fr)=="3"] <- "Europe Ecologie - Les Verts"
levels(df_clean$party_choice_fr)[levels(df_clean$party_choice_fr)=="4"] <- "Les Republicains"
levels(df_clean$party_choice_fr)[levels(df_clean$party_choice_fr)=="5"] <- "La France insoumise de Jean-Luc Melenchon"
levels(df_clean$party_choice_fr)[levels(df_clean$party_choice_fr)=="6"] <- "Parti socialiste"
levels(df_clean$party_choice_fr)[levels(df_clean$party_choice_fr)=="7"] <- "Debout la France"
levels(df_clean$party_choice_fr)[levels(df_clean$party_choice_fr)=="8"] <- "Parti communiste francais"
levels(df_clean$party_choice_fr)[levels(df_clean$party_choice_fr)=="9"] <- "Other"
levels(df_clean$party_choice_fr)[levels(df_clean$party_choice_fr)=="10"] <- NA

final <- df_clean  %>%   
 select(uuid, hCountry, VIGNETTE, Q17, Choice, pipe_Choice_Pipe, Q18,
 matches("CJ.ATT.CH."),
c("Q15A1r1", "Q15A2r1", 
"Q15A3r1", "Q15A4r1", "Q15A5r1", "Q15A6r1",
 "Q15B1r1", "Q15B1r2", "Q15B2r1", "Q15B2r2", 
 "Q15B3r1", "Q15B3r2", "Q15B4r1", "Q15B4r2", "Q15B5r1", "Q15B5r2", 
 "Q15B6r1", "Q15B6r2", "CJ_1_timer", "CJ_2_timer", "CJ_3_timer", 
 "CJ_4_timer", "CJ_5_timer", "CJ_6_timer"),
Q19r1,  Q20C, Q20D, Q20E, Q20F, Q21r6, Q21r7, Q21r8, Q21r9, Q21r10, Q13, Q13_FR, ParteDisplay_DE, ParteDisplay_FR, Q20B, Q20A, 
party_choice_fr, party_choice_de, Job_situation, Household_Income, Gender, Age, Education, region_fr, region_de)

saveRDS(final, "data/arms_conjoint.RDS")
